// Created by carolsail

import { ENUM_AUDIO_CLIP, ENUM_UI_TYPE } from "../Enum";
import { StaticInstance } from './../StaticInstance';
import AudioManager from "../manager/AudioManager";
import BaseLayer from "./Baselayer";
import SdkManager from "../manager/SdkManager";
import ToastManager from "../manager/ToastManager";
import { ICallBack } from "../shared/common/types";
import DataManager from "../manager/DataManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class MenuLayer extends BaseLayer {

    btnStart: cc.Node = null
    btnSetting: cc.Node = null
    btnGames: cc.Node = null
    btnShare: cc.Node = null
    btnSkin: cc.Node = null
    btnRank: cc.Node = null
    btnMultiple: cc.Node = null

    onLoad() {
        this.btnStart = cc.find('main/btn_start', this.node)
        this.btnMultiple = cc.find('main/btn_multiple', this.node)
        this.btnSetting = cc.find('bottom/btn_setting', this.node)
        this.btnGames = cc.find('bottom/btn_games', this.node)
        this.btnShare = cc.find('bottom/btn_share', this.node)
        this.btnSkin = cc.find('right/btn_skin', this.node)
        this.btnRank = cc.find('right/btn_rank', this.node)
        this.btnStart.on('click', this.onStartClick, this)
        this.btnMultiple.on('click', this.onMultipleClick, this)
        this.btnSetting.on('click', this.onSettingClick, this)
        this.btnGames.on('click', this.onGamesClick, this)
        this.btnShare.on('click', this.onShareClick, this)
        this.btnSkin.on('click', this.onSkinClick, this)
        this.btnRank.on('click', this.onRankClick, this)
    }

    onDestroy() {
        this.btnStart.off('click', this.onStartClick, this)
        this.btnMultiple.off('click', this.onMultipleClick, this)
        this.btnSetting.off('click', this.onSettingClick, this)
        this.btnGames.off('click', this.onGamesClick, this)
        this.btnShare.off('click', this.onShareClick, this)
        this.btnSkin.off('click', this.onSkinClick, this)
        this.btnRank.off('click', this.onRankClick, this)
    }

    onEnable() { }

    onDisable() { }

    onStartClick() {
        AudioManager.instance.playSound(ENUM_AUDIO_CLIP.CLICK)
        StaticInstance.uiManager.toggle(ENUM_UI_TYPE.MENU, false)
        StaticInstance.uiManager.toggle(ENUM_UI_TYPE.READY)
    }

    onSettingClick() {
        AudioManager.instance.playSound(ENUM_AUDIO_CLIP.CLICK)
        StaticInstance.uiManager.toggle(ENUM_UI_TYPE.MENU, false)
        StaticInstance.uiManager.toggle(ENUM_UI_TYPE.SETTING_MENU)
    }

    onGamesClick() {
        AudioManager.instance.playSound(ENUM_AUDIO_CLIP.CLICK)
        StaticInstance.uiManager.toggle(ENUM_UI_TYPE.MENU, false)
        StaticInstance.uiManager.toggle(ENUM_UI_TYPE.MORE)
    }

    onShareClick() {
        AudioManager.instance.playSound(ENUM_AUDIO_CLIP.CLICK)
        if (SdkManager.instance.getPlatform()) {
            SdkManager.instance.activeShare()
        } else {
            ToastManager.instance.show('仅支持小游戏平台', { gravity: 'TOP', bg_color: cc.color(226, 69, 109, 255) })
        }
    }

    onSkinClick() {
        AudioManager.instance.playSound(ENUM_AUDIO_CLIP.CLICK)
        StaticInstance.uiManager.toggle(ENUM_UI_TYPE.MENU, false)
        StaticInstance.uiManager.toggle(ENUM_UI_TYPE.SKIN)
    }

    onRankClick() {
        AudioManager.instance.playSound(ENUM_AUDIO_CLIP.CLICK)
        StaticInstance.uiManager.toggle(ENUM_UI_TYPE.MENU, false)
        StaticInstance.uiManager.toggle(ENUM_UI_TYPE.RANK)
    }

    onMultipleClick() {
        AudioManager.instance.playSound(ENUM_AUDIO_CLIP.CLICK)
        // 直接登录或缓存登录
        this.onLogin((obj) => {
            if (obj.status) {
                StaticInstance.uiManager.toggle(ENUM_UI_TYPE.MENU, false)
                StaticInstance.uiManager.toggle(ENUM_UI_TYPE.HALL)
            } else {
                ToastManager.instance.show(obj.msg, { gravity: 'TOP', bg_color: cc.color(226, 69, 109, 255) })
            }
        })
    }

    onLogin(callback?: (params: ICallBack) => void) {
        const back: ICallBack = { status: false, data: null, msg: '登录失败' }
        if (!(DataManager.instance.user && DataManager.instance.user.openid)) {
            if (cc.sys.platform == cc.sys.WECHAT_GAME) {
                // 跳转授权确认页面
                StaticInstance.uiManager.toggle(ENUM_UI_TYPE.AUTH_NOTICE)
            } else {
                SdkManager.instance.getLoginInfoLocal(info => {
                    DataManager.instance.user = info.data
                    DataManager.instance.save()
                    back.status = true
                    back.data = DataManager.instance.user
                    back.msg = "直接登录：登录成功"
                    callback && callback(back)
                })
            }
        } else {
            back.status = true
            back.data = DataManager.instance.user
            back.msg = '缓存信息：登录成功'
            callback && callback(back)
        }
    }
}
